package com.dzt.cloudreader.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.dzt.cloudreader.R;
import com.dzt.cloudreader.adapter.DouBanTopAdapter;
import com.dzt.cloudreader.base.TitleActivity;
import com.dzt.cloudreader.model.HotMovieBean;
import com.dzt.cloudreader.network.TopMoviePresenter;
import com.dzt.cloudreader.network.TopMovieView;
import com.dzt.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class DoubanTopActivity extends TitleActivity {

    @BindView(R.id.xrv_top)
    XRecyclerView recyclerView;
    private DouBanTopAdapter adapter;
    private List<HotMovieBean.SubjectsBean> lists = new ArrayList<>();
    private TopMoviePresenter presenter;
    private int mStart = 0;
    private int mCount = 21;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_douban_top;
    }

    @Override
    protected void initParams() {
        setToolBarTitle("豆瓣电影Top250");
        adapter = new DouBanTopAdapter(this, lists, R.layout.item_douban_top);
        initRecyclerView();
        presenter = new TopMoviePresenter(context);
        presenter.onCreate();
        presenter.attachView(topMovieView);
        loadDouBanTop250();
    }

    private void initRecyclerView() {
        recyclerView.setPullRefreshEnabled(false);
        recyclerView.setLoadingMoreEnabled(true);
        recyclerView.clearHeader();
        //构造器中，第一个参数表示列数或者行数，第二个参数表示滑动方向,瀑布流
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,
                StaggeredGridLayoutManager.VERTICAL));
        // 需加，不然滑动不流畅
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setHasFixedSize(false);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                mStart += mCount;
                loadDouBanTop250();
            }
        });
    }

    private void loadDouBanTop250() {
        presenter.getMovieTop250(mStart, mCount);
    }

    @Override
    protected void onRefresh() {
        super.onRefresh();
        loadDouBanTop250();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }

    private TopMovieView topMovieView = new TopMovieView() {

        @Override
        public void onSuccess(HotMovieBean movie) {
            showContentView();
            if (movie != null && movie.getSubjects().size() != 0) {
                recyclerView.refreshComplete();
                lists.addAll(movie.getSubjects());
                adapter.notifyDataSetChanged();
            } else {
                recyclerView.noMoreLoading();
            }
        }

        @Override
        public void onError(String result) {
            recyclerView.refreshComplete();
        }
    };

    public static void start(Context mContext) {
        Intent intent = new Intent(mContext, DoubanTopActivity.class);
        mContext.startActivity(intent);
    }
}
